package com.kjjt.service;

import com.kjjt.entity.UserDemo;
import com.kjjt.exception.BizException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    private static final Map<String, UserDemo> USER_MAP = new HashMap<>();

    public void addUser(UserDemo user) {
        if (USER_MAP.containsKey(user.getUsername())) {
            throw new BizException("用户名重复了");
        }
        USER_MAP.put(user.getUsername(), user);
    }

    public UserDemo getUser(String username) {
        return USER_MAP.get(username);
    }

    public void deleteUser(String username) {
        USER_MAP.remove(username);
    }

    public void updateUser(UserDemo user) {
        UserDemo oldUser = USER_MAP.get(user.getUsername());
        if (oldUser == null) {
            throw new BizException("无法更新，用户不存在！");
        }

        USER_MAP.replace(user.getUsername(), user);
    }

    public List<UserDemo> getAll() {
        return new ArrayList<>(USER_MAP.values());
    }

}
